<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 员工信息
                </el-breadcrumb-item>

                <el-button style="margin-left: 90%"
                    type="text"
                    icon="el-icon-refresh"
                    @click="getData"
                >刷新
                </el-button>

            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>

                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">添加</el-button>
            </div>
            <el-table
                :data="StaffTableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="用户名" width="150" align="center"></el-table-column>
                <el-table-column label="性别" width="150" align="center">
                    <template slot-scope="scope">{{ scope.row.sex === 1 ? '男' : (scope.row.sex === 2 ? '女' : '未知') }}
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="生日" align="center"></el-table-column>

                <el-table-column prop="addTime" label="添加时间" align="center"></el-table-column>
                <el-table-column label="底薪" align="center" width="150">
                    <template slot-scope="scope">
                        ￥{{ scope.row.money }}

                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.state"
                            :active-value="0"
                            :inactive-value="1"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            @change="changeSwitch(scope.row)" />
                    </template>

                    <!--                    <template slot-scope="scope">-->
                    <!--                        <el-tag-->
                    <!--                            :type="scope.row.state=='0'?'success':'danger'"-->
                    <!--                        >{{scope.row.state===0?"正常":'停用' }}-->
                    <!--                        </el-tag>-->
                    <!--                    </template>-->
                </el-table-column>
                <el-table-column label="操作" width="250" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-circle-plus"
                            @click="handleSales(scope.row.id,scope.row.name)"
                        >添加销售
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 弹窗 -->
        <el-dialog title="添加员工" :visible.sync="addVisible" width="30%">
            <el-form ref="user" :model="user" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="user.name"></el-input>
                </el-form-item>

                <el-form-item label="性别">
                    <el-select v-model="user.sex" placeholder="请选择">
                        <el-option key="1" label="男" :value="1"></el-option>
                        <el-option key="2" label="女" :value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="生日">

                    <el-date-picker
                        type="date"
                        placeholder="选择日期"
                        v-model="user.birthday"
                        value-format="yyyy-MM-dd"
                        style="width: 100%;">
                    </el-date-picker>

                </el-form-item>
                <el-form-item label="底薪">
                    <el-input type='number' maxLength='6' v-model="user.money"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="saveUser">确 定</el-button>
            </span>
        </el-dialog>
        <!--添加业绩弹窗-->
        <el-drawer
            :direction=direction
            :visible.sync="drawer"
            style="overflow: auto"
            :with-header="false" size="30%">
            <div style="margin-top: 2%">
                <div style="margin-left: 37%">
                    <h3 style="color: #606266">添加{{name}}伴手礼销售</h3>
                </div>

                <el-form status-icon :model="salesHandData" ref="ruleForm" label-width="25%" class="demo-ruleForm"
                         style="margin-left: 8%;margin-top: 8%;height: 20%">
                    <el-form-item label="会员卡充值"  prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="saveSalesForm" @keyup.esc.native="saveSalesForm" placeholder="会员卡充值" v-model="salesHandData.vipMoney"
                                  autocomplete="off" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="销量时间">
                        <el-date-picker
                            type="date"
                            placeholder="选择日期"
                            v-model="salesHandData.date"
                            value-format="yyyy-MM-dd"
                            style="width: 80%;">
                        </el-date-picker>
                    </el-form-item>

                    <el-col v-for="item in handList" :key="item.index" >
                        <el-form-item class="block" :label="item.name" prop="code">
                            <el-input  @keyup.enter.native="saveSalesForm" @keyup.esc.native="saveSalesForm"  :placeholder="item.name" type="text" v-model="item.num" style="width: 80%" p></el-input>
                        </el-form-item>
                    </el-col>


                    <el-form-item style="margin-left: 44%;margin-top: 3%">
                        <el-button @click="cancelSalesFrom">取消</el-button>
                        <el-button type="primary" @click="saveSalesForm">保存</el-button>
                    </el-form-item>
                </el-form>

            </div>
        </el-drawer>
        </div>
</template>

<script>
import { getLisUser, addUser, getUserInfo, delInfo } from '@/api/staff';
import { getLisProduct } from '@/api/product';
import { addUpdate } from '@/api/SalesHandRecode';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            zone: {  },
            StaffTableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            user: {},
            idx: -1,
            id: -1,
            ids: {
                ids: []
            },
            value: true,
            drawer:false,
            direction:'rtl',
            salesHandData:{
                date:new Date()
            },
            name:'',
            handQueryList:{
                pageIndex: 1,
                pageSize: 90,
                categoryId:11

            },
            handList:[
            ],
        };
    },
    created() {
        this.getData();
    },
    methods: {
        saveSalesForm(){
            this.salesHandData.salesRecode=JSON.stringify(this.handList)
            addUpdate(this.salesHandData).then(res => {
                if (res.success) {
                    this.addVisible = false;
                    this.$message.success('保存成功！');
                    this.drawer=false
                    this.salesHandData={
                        date: this.salesHandData.date
                    }


                } else {
                    this.$message.warning(res.message);
                }


            })
            console.log(this.salesHandData)

        },

        cancelSalesFrom(){
            this.salesHandData={}
            this.drawer=false

        },
        //打开添加业绩窗口
        handleSales(id,name){
            getLisProduct(this.handQueryList).then(res => {
                this.salesHandData.userId=id
                this.name=name
                this.drawer=true
                let arr=[]
                res.data.records.forEach(function(sales){
                    let sa={}
                    sa.id=sales.id
                    sa.name=sales.productName
                    sa.num=''
                    arr.push(sa)
                })
                this.handList=arr

            })
        },
        //更改用户状态
        changeSwitch(data) {
            this.user = {};
            this.user.id = data.id;
            this.user.state = data.state;
            addUser(this.user).then(res => {
                this.user = {};
                if (res.success) {
                    if (data.state === 0) {
                        this.$message.success('已启用');
                    } else {
                        this.$message.warning('已停用');
                    }
                } else {
                    this.$message.success(res.message);
                }
            }).catch(() => {
                this.$message.error('网络异常');
            })

        },

        //保存用户
        saveUser() {
            addUser(this.user).then(res => {
                if (res.success) {
                    this.addVisible = false;
                    this.$message.success('保存成功！');
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }

            }).catch(() => {
                this.$message.error('网络异常');
            })


        },
        //打开添加弹窗
        handleAdd() {
            this.user = {};
            this.addVisible = true;

        },
        // 获取 easy-mock 的模拟数据\
        getData() {
            getLisUser(this.query).then(res => {
                if (res.success){
                    this.StaffTableData = res.data.records;
                    this.pageTotal = res.data.total || 0;
                }else {
                    console.log(res)
                    this.$message.warning(res.message)
                    if (res.code===10005){
                        this.$router.push({path:'/login'});
                    }


                }

            });
        },
        //取消按钮
        cancel(){
            this.addVisible = false;
            //清空对象
            this.user={}
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(userId) {
            if (userId!==undefined){
                this.ids.ids=[]
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(userId);
                delInfo(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }
                })

            })
                .catch(() => {
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id)

            }
            this.handleDelete()



        },
        // 编辑操作
        handleEdit(id) {
            this.addVisible = true;
            this.user.id = id;
            getUserInfo(this.user).then(res => {
                this.user = res.data;
            });


        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style >
.el-drawer{

    overflow: scroll
}

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
